//
//  SLOriginalView.swift
//  langligelang
//
//  Created by 秦－政 on 2016/10/26.
//  Copyright © 2016年 pete. All rights reserved.
// 原创微博自定义View

import UIKit

class SLOriginalView: UIView {
    // 记录原创微博的底部约束
    var originalViewBottomeCons: Constraint?
    // 定义一个属性ViewModel 工外界赋值
    var statusViewModel:SLStatusViewModel?{
        didSet{
            // cell 给它赋过值以后他将自己内部的控件赋值
            // 头像
            headImageView.sl_setImage(withUrlString: statusViewModel?.statusModel?.user?.profile_image_url, placeholderImgName: "avatar_default_big")
            // 昵称
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            // 等级
            membershipImageView.image = statusViewModel?.mbrankImage
            // 认证
            avatarImageView.image = statusViewModel?.verifiedImage
            // 微博正文
//            contentLabel.text = statusViewModel?.statusModel?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedStr
            // 来源
            sourceLabel.text = statusViewModel?.sourceStr
            // 时间
            timeLabel.text = statusViewModel?.timeStr
            // 卸载约束
            originalViewBottomeCons?.uninstall()
            // 判断是否有图
            if (statusViewModel?.statusModel?.pic_urls?.count)! > 0 {
                // 配图赋值
                pictureView.picUrls = statusViewModel?.statusModel?.pic_urls
                pictureView.isHidden = false
                self.snp_makeConstraints { (make) in
                    self.originalViewBottomeCons = make.bottom.equalTo(pictureView).offset(10).constraint
                }
            }else{
                // 没有图
                pictureView.isHidden = true
                self.snp_makeConstraints(closure: { (make) in
                self.originalViewBottomeCons = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.white
        // 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(membershipImageView)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
//        print(pictureView)
        // 布局
        // 02 - 添加约束
        headImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 35, height: 35))
            make.top.left.equalTo(10)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(10)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(10)
        }
        
        membershipImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(10)
        }
        
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_right)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.left.equalTo(headImageView)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.left.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) in
          self.originalViewBottomeCons = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        //  点击高亮效果执行的闭包
        //  (view, attributedString, range, rect)
        //  1. view : 显示富文本的容器, YYLabel
        //  2. attributedString: 显示的富文本
        //  3. range  : 点击高亮的范围
        //  4. rect:  点击的高亮的区域
        contentLabel.highlightTapAction = { (view, attributedString, range, rect) -> Void in
            //  根据高亮的范围获取对应的字符串
            let text = (attributedString.string as NSString).substring(with: range)
            print(text)
        }
    }
    // MARK: - 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default_big")
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.blue, fontSize: SLNormalFontSize, text: "恩恩")
    // 时间
    private lazy var timeLabel:UILabel = UILabel(textColor: SLTHEMECOLOR, fontSize: SLSmallFontSize, text: "时间")
    // 来源
    private lazy var sourceLabel:UILabel = UILabel(textColor: SLTHEMECOLOR, fontSize: SLSmallFontSize, text: "来自小哥哥")
    // 微博等级
    private lazy var membershipImageView:UIImageView = UIImageView(imgName: "common_icon_membership")
    // 认证
    private lazy var avatarImageView:UIImageView = UIImageView(imgName: "avatar_vgirl")
    // 微博正文
//    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.black, fontSize: SLNormalFontSize, text: "", maxWidth: SLSCREENW - 20)  // 使用UILabel
    private lazy var contentLabel: YYLabel = {
        let label = YYLabel()
        // 指定最大换行宽度
        label.preferredMaxLayoutWidth = SLSCREENW - 20
        // 换行
        label.numberOfLines = 0
        return label
    }()// 使用YYText

    // 配图
    private lazy var pictureView: SLStatusPictureView = {
        let view = SLStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
